package com.hw.one.collect.client;

import com.hw.one.collect.data.DataService;
import com.hw.one.collect.model.DataServiceType;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

/**
 * Created by qunxing.du on 2016/7/12.
 * Copyright 2016 汉威.智慧环保事业部 Limited.
 * All rights reserved.
 */
public class DataServiceFactory {

    private static DataServiceFactory dataServiceFactory;
    private DataServiceFactory(){}

    public static DataServiceFactory newInstance(){
        if(dataServiceFactory == null){
            dataServiceFactory = new DataServiceFactory();
        }
        return dataServiceFactory;
    }

    public synchronized String getZzCityDataService(String params){
        String[] paramsArr = StringUtils.split(params, "&");
        int isSrc = -1, dataType = -1;
        for (String param : paramsArr) {
            String name = param.split("=")[0];
            String value = param.split("=")[1];
            if ("isSrc".equals(name)) {
                isSrc = NumberUtils.toInt(value);
                continue;
            }
            if ("dataType".equals(name)) {
                dataType = NumberUtils.toInt(value);
                continue;
            }
        }
        return  getZzCityDataService(isSrc,dataType);
    }

    public synchronized String getZzCityDataService(int isSrc, int dataType){
        //获取数据类型
        int type = getType(isSrc, dataType);
        return getDataService("zzCity",type);

    }

    /**
     * 获取数据类型
     * @param isSrc
     * @param dataType
     * @return
     */
    public synchronized int getType(int isSrc, int dataType) {

        if (isSrc == 1 && dataType == 0) {
            return DataServiceType.RAW_RT.getValue();
        }
        if (isSrc == 1 && dataType == 1) {
            return DataServiceType.RAW_FIVEMIN.getValue();
        }
        if (isSrc == 1 && dataType == 2) {
            return DataServiceType.RAW_ONEHOUR.getValue();
        }
        if (isSrc == 1 && dataType == 3) {
            return DataServiceType.RAW_DAYAVG.getValue();
        }
        if (isSrc == 0 && dataType == 0) {
            return DataServiceType.AUDIT_RT.getValue();
        }
        if (isSrc == 0 && dataType == 1) {
            return DataServiceType.AUDIT_FIVEMIN.getValue();
        }
        if (isSrc == 0 && dataType == 2) {
            return DataServiceType.AUDIT_ONEHOUR.getValue();
        }
        if (isSrc == 0 && dataType == 3) {
            return DataServiceType.AUDIT_DAYAVG.getValue();
        }

        return DataServiceType.NULL.getValue();
    }

    public synchronized String getDataService(String prefix,int type){
        String serviceName;
        switch (type) {
            case 0:
                serviceName =  DataServiceType.RAW_RT.getDesc();
                break;
            case 1:
                serviceName =  DataServiceType.RAW_FIVEMIN.getDesc();
                break;
            case 2:
                serviceName =  DataServiceType.RAW_ONEHOUR.getDesc();
                break;
            case 3:
                serviceName =  DataServiceType.RAW_DAYAVG.getDesc();
                break;
            case 4:
                serviceName =  DataServiceType.AUDIT_RT.getDesc();
                break;
            case 5:
                serviceName =  DataServiceType.AUDIT_FIVEMIN.getDesc();
                break;
            case 6:
                serviceName =  DataServiceType.AUDIT_ONEHOUR.getDesc();
                break;
            case 7:
                serviceName =  DataServiceType.AUDIT_DAYAVG.getDesc();
                break;
            default:
                serviceName =  DataServiceType.NULL.getDesc();
                break;
        }
        return prefix + serviceName;
    }
}
